Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

retry

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

retry

Abstraction for exponential and custom retry strategies for failed operations.

  • 0.13.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is retry?

The retry npm package provides a set of functions for implementing retry behavior in your code. This is particularly useful when dealing with operations that may fail due to transient errors, such as network requests, database operations, or any other tasks that might not succeed on the first attempt. The package allows you to customize the retry strategy, including the number of attempts, the interval between attempts, and the criteria for retrying.

What are retry's main functionalities?

Simple retry operation

This code sample demonstrates how to perform a simple retry operation. It uses the `operation.attempt` method to try an operation that might fail. If the operation fails, it uses `operation.retry` to determine if a retry should be attempted based on the current strategy.

const retry = require('retry');

const operation = retry.operation();

operation.attempt(currentAttempt => {
  performSomeOperation(error => {
    if (error) {
      if (operation.retry(error)) {
        return;
      }
      console.error('Operation failed:', error);
    } else {
      console.log('Operation succeeded');
    }
  });
});

Custom retry strategy

This code sample shows how to set up a custom retry strategy with specific options such as the number of retries, the backoff factor, and the minimum and maximum timeout between retries. The `randomize` option adds some randomness to the retry intervals to prevent overloading the server.

const retry = require('retry');

const operation = retry.operation({
  retries: 5,
  factor: 3,
  minTimeout: 1 * 1000,
  maxTimeout: 60 * 1000,
  randomize: true
});

operation.attempt(currentAttempt => {
  performSomeOperation(error => {
    if (error) {
      if (operation.retry(error)) {
        return;
      }
      console.error('Operation failed after ' + currentAttempt + ' attempts:', error);
    } else {
      console.log('Operation succeeded on attempt ' + currentAttempt);
    }
  });
});

Other packages similar to retry

FAQs

Package last updated on 21 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc